<?php

/**
 * Extra useful functions found on the internet
 * 
 * Retrieved May 19, 2011
 * http://code.google.com/p/citation-bot/source/browse/trunk/wikiFunctions.php?spec=svn228&r=228
 * @author MartinS 
 * @license http://creativecommons.org/licenses/by-sa/3.0/ CC BY-SA 3.0
 * @since v.0.1.0
 * @package rappelz-hunbot
 */

// Code released under the creative commons CC BY-SA 3.0 license
// http://creativecommons.org/licenses/by-sa/3.0/

// Retrieved May 19, 2011
// http://code.google.com/p/citation-bot/source/browse/trunk/wikiFunctions.php?spec=svn228&r=228

define ('template_regexp', '~\{\{\s*([^\|\}]+)(.*)\}\}~ms');

//$MY_TEMPLATE_REGEXP 	= "~\{\{\s*([^\|\}]+)([^\{]|\{[^\{])*?\}\}~ms";
//$MY_TEMPLATE_REGEXP 	= "~\{\{\s*([^\|\}]+)(.*)\}\}~ms";

// Retrieved May 19, 2011
// Extract template
// Pass the code to find the template in, and the name of the template (with spaces, not underscores, if appropriate)


/**
 * class extra_functions
 * Extra functions to help work with wikimedia templates
 * 
 * @package rappelz-hunbot
 */
class extra_functions {
static function extract_template($code, $target) {
	global $MY_TEMPLATE_REGEXP;
	$placeholder = "!-TEMPLATE PLACEHOLDER TP%s-!";
	$placeholder_regexp = "~$placeholder~";

//	print_r($MY_TEMPLATE_REGEXP);
//	print_r($code);

	if (preg_match(template_regexp, $code, $match)) {
		//print "preg_match: $match[1]::$target::\n";
		if(strtolower(trim($match[1])) == strtolower($target)
			|| strtolower(trim($match[1])) == strtolower("Template:".$target)) {
			return $match[0];
		}
	}
	return false;
/*	array_shift($match);
	print_r($match);
	return false;
*/

	$i = 0;
	while (preg_match(template_regexp, $code, $match)) {
		
		$template[$i] = $match[0];
		$template_name = str_replace("_", " ", trim($match[1]));

		if (strtolower($template_name) == strtolower($target)) {
			$return = $template[$i];
			while (preg_match(sprintf($placeholder_regexp, "(\d+)"), $return, $match)) {
				$template_n = $match[1];
				$return = preg_replace(sprintf($placeholder_regexp, $template_n), $template[$template_n], $return);
			}
				return $return;
		}

		$code = str_replace($template[$i], sprintf($placeholder, $i), $code);
		++$i;
	}
	return false;
}


// Retrieved May 19, 2011
// http://code.google.com/p/citation-bot/source/browse/trunk/wikiFunctions.php?spec=svn228&r=228
// Extracts parameters in a Wikipedia template.
// Returns the parameters as an array (
// "parameter_name" => Array (value, equals sign, pipe)
// )
// Test cases should include comments with multiple pipes spanning multiple lines and including wikilinks

static function extract_parameters($template) {
  // First, replace pipes that don't mark parameter boundaries with !-PIPE PLACEHOLDER-!
  $pipe_placeholder = "!-PIPE PLACEHOLDER pp-!";
  // This will include pipes in [[Wikilinks|]]:
  $wikilink_regexp = "~(\[\[[^\]]+)\|([^\]]+\]\])~";
  //  and in <!-- comments -->
  $comment_regexp = "~(<!--.*?)\|(.*?-->)~";

  // Remove whitespace and braces from template
  $template = trim($template);
  $template = substr($template, 2, strlen($template)-4);
  // Replace pipes with placeholders in comments and links
  $template = preg_replace($wikilink_regexp, "$1$pipe_placeholder$2", $template);
  while (preg_match($comment_regexp, $template)) {
    $template = preg_replace($comment_regexp, "$1$pipe_placeholder$2", $template);
  }

  // Replace templates with placeholders
  $template_placeholder = "!-TEMPLATE PLACEHOLDER TP%s-!";
  $template_placeholder_regexp = "~$template_placeholder~";
  #$template_regexp = "~\{\{\s*[^\|\}]+([^\{]|\{[^\{]|\{\{[^/}]+\}\})*?\}\}~";

  $i = 0;
  while (preg_match(template_regexp, $template, $match)) {
    $subtemplate[++$i] = $match[0];
    $template = str_replace($subtemplate[$i], sprintf($template_placeholder, $i), $template);
  }
  $splits = preg_split("~(\s*\|\s*)~", $template, -1, PREG_SPLIT_DELIM_CAPTURE);
  
  // The first line doesn't contain a parameter; it's the template name
  $i = 0;
  foreach ($splits as $split) {
    ++$i;
    if ($i % 2) {
      $lines[$i / 2] = $split;
    } else {
      $pipe[($i+1) / 2] = $split;
    }
  }
  unset($lines[0]);

  $unnamed_parameter_count = 0;
  foreach ($lines as $i => $line) {
    preg_match("~^([^= ]*)(\s*=\s*)?([\s\S]*)$~", $line, $match);
    if ($match[2]) {
      // then an equals sign is present; i.e. we have a named parameter
      $value = $match[3];
      $parameter_name = $match[1];
    } else {
      $value = $match[1];
      $parameter_name = "unnamed_parameter_" . ++$unnamed_parameter_count;
    }
    // Restore templates that were replaced with placeholders
    while (preg_match(sprintf($template_placeholder_regexp, "(\d+)"), $value, $sub_match)) {
      $template_n = $sub_match[1];
      $value = preg_replace(sprintf($template_placeholder_regexp, $template_n), $subtemplate[$template_n], $value);
    }
    $parameters[$parameter_name] = Array(str_replace($pipe_placeholder, "|", $value), $pipe[$i], $match[2]);
  }
  return $parameters;
}
}
?>